﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DomesticPestLib;
using System.Collections;

namespace DomesticPestProject
{
    public partial class frmCommodity : System.Web.UI.Page
    {
        DLayer dlayer;
        ArrayList objalst;
        entyCommodity entyCom;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnCommodity.Enabled = false;
                pnlAdd.Visible = false;
                BindDetails();
            }
           
        }

        private void BindDetails()
        {

            {
                entyCom = new entyCommodity();
                dlayer = new DLayer();
                objalst = new ArrayList();
                objalst = dlayer.selectCommodity();
                gvComm.DataSource = objalst;
                gvComm.DataBind();
                //DataList1.DataSource = objalst;
                //DataList1.DataBind();
            }

        }

        protected void btnInformation_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmInformation.aspx");
        }

        protected void btnMain_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmMain.aspx");
        }

        protected void btnCommodity_Click(object sender, EventArgs e)
        {

        }

        protected void btnTransaction_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmTransaction.aspx");
        }

        protected void btnPest_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPest.aspx");
        }

        protected void btnLctn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmLoc.aspx");
        }

        protected void btnPsDsrbtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("frmPestDistribution.aspx");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            pnlshow.Visible = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }

        protected void gvComm_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string bcode = gvComm.Rows[e.RowIndex].Cells[2].Text;
            (new DLayer()).DeleteComm(bcode);
            BindDetails();
        }

        protected void gvComm_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvComm.EditIndex = e.NewEditIndex;
            BindDetails();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            entyCom = new entyCommodity();
            entyCom.BCode = txtByrCd.Text;
            entyCom.Category = txtAddCategory.Text;
            entyCom.CName = txtAddCommonNm.Text;
            entyCom.SName = txtAddSciName.Text;
            entyCom.Synonym = txtAddSynonym.Text;
            entyCom.SubSpcs = txtAddSubSpc.Text;
            entyCom.FName = txtAddFNm.Text;
            entyCom.OName = txtOdrNm.Text;
            (new DLayer()).SaveComm(entyCom);
            BindDetails();
            clear();
            pnlAdd.Visible = true;
        }
        private void clear()
        {
            txtAddOrderNm.Text = "";
            txtAddCategory.Text = "";
            txtAddCommonNm.Text="";
            txtAddFNm.Text = "";
            txtAddSciName.Text = "";
            txtAddSubSpc.Text = "";
            txtAddSynonym.Text = "";
            txtByrCd.Text = "";
        }
        protected void gvComm_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvComm.EditIndex = -1;
            BindDetails();
        }

        protected void gvComm_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            entyCommodity com = new entyCommodity();
            TextBox txtByrCd = gvComm.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
            TextBox txtAddCategory = gvComm.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
            TextBox txtAddCommonNm = gvComm.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
            TextBox txtAddSciName = gvComm.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
            TextBox txtSynm = gvComm.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;
            TextBox txtAddSubSpc = gvComm.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox;
            TextBox txtAddFNm = gvComm.Rows[e.RowIndex].Cells[8].Controls[0] as TextBox;
            TextBox txtAddOrderNm = gvComm.Rows[e.RowIndex].Cells[9].Controls[0] as TextBox;

            com.BCode = txtByrCd.Text;
            com.Category = txtAddCategory.Text;
            com.CName = txtAddCommonNm.Text;
            com.SName = txtAddSciName.Text;
            com.Synonym = txtSynm.Text;
            com.SubSpcs = txtAddSubSpc.Text;
            com.FName = txtAddFNm.Text;
            com.OName = txtAddOrderNm.Text;
            DLayer d = new DLayer();
            (new DLayer()).UpdateComm(com);
            gvComm.EditIndex = -1;
            BindDetails();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            clear();
        }
    }
}